AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

/*---------------------------------------------------------
   Name: ENT:Initialize()
---------------------------------------------------------*/
function ENT:Initialize()

	self.Owner = self.Entity:GetOwner()

	if !ValidEntity(self.Owner) then
		self:Remove()
		return
	end

	self.Entity:SetModel("models/items/ar2_grenade.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:SetKeyValue("physdamagescale", "9999")
	self.Entity:DrawShadow(false)
	
	local phys = self.Entity:GetPhysicsObject()
	
	if (phys:IsValid()) then
		phys:Wake()
	end

	self.Entity:EmitSound("Weapon_FlareGun.Burn")
	
	self.Timer = CurTime() + 1
	self.RepeatTimer = CurTime() + 1

	self.Entity:SetNWBool("Smoke", true)
	self.Entity.Explode = false
end

/*---------------------------------------------------------
   Name: ENT:Think()
---------------------------------------------------------*/
function ENT:Think()

	if self.RepeatTimer < CurTime() then
		local effectdata = EffectData()
			effectdata:SetOrigin(self.Entity:GetPos())
		util.Effect("effect_mad_flare_fire", effectdata)
	end

	if self.Timer < CurTime() then
		self:Explosion()
		self.Timer = CurTime() + 10
	end

	if self.Entity:WaterLevel() > 2 then
		self.Entity:Remove()
	end
end

/*---------------------------------------------------------
   Name: ENT:Explosion()
---------------------------------------------------------*/
function ENT:Explosion()

	self.Entity.Explode = true

	local effectdata = EffectData()
		effectdata:SetOrigin(self.Entity:GetPos())
	util.Effect("effect_mad_flare_explosion", effectdata)

	local explo = ents.Create("env_explosion")
		explo:SetOwner(self.Owner)
		explo:SetPos(self.Entity:GetPos())
		explo:SetKeyValue("iMagnitude", "50")
		explo:SetKeyValue("spawnflags", "783")
		explo:Spawn()
		explo:Activate()
		explo:Fire("Explode", "", 0)

	self.Entity:Fire("kill", "", 5)
end

function ENT:PhysicsCollide(data, physobj)
	if !self.DidHit then
		self.Entity:GetPhysicsObject():EnableMotion(false)
		self.Entity:SetKeyValue("solid", "0")
		self.lolHitPos = data.HitPos
		// routed the blowing itself up into the physicsupdate function to prevent the "YOU GONNA GET CRASHED" spam of bullshit and lies.
  		self.DidHit = true
		self.Entity:GetPhysicsObject():SetVelocity(Vector(0,0,0))
		if (data.HitEntity!=nil) then
			if (ValidEntity(data.HitEntity)) then
				self.Fuckit=true
				if (data.HitEntity:IsPlayer()) then
					// because kills from non-lethal weapons are teh sex.
					data.HitEntity:TakeDamage(10, self.Entity:GetOwner(), self.Entity)
					// poison them if its from an upgraded dart gun
					BurnPlayer(data.HitEntity, 10, self.Entity:GetOwner(), self.Entity:GetOwner():GetWeapon("weapon_flaregun"))
				else
					// make it still be effective against NPCs (and vehicles)
					data.HitEntity:TakeDamage(30, self.Entity:GetOwner(), self.Entity)
				end
			end
		end
	end
end 

function ENT:PhysicsUpdate()
	local trace = {}
		trace.start = self.Entity:GetPos()
		trace.endpos = self.Entity:GetPos()
		trace.filter = {self.Entity, self.Entity:GetOwner()}
	trace = util.TraceLine(trace)
	if trace.Hit then self.Fuckit=true end
	if (self.DidHit == true) then
		self.Entity:SetPos(self.lolHitPos)
	end
	if (self.Fuckit == true) then
		self.Entity:Remove()
	end
end

/*---------------------------------------------------------
   Name: ENT:OnRemove()
---------------------------------------------------------*/
function ENT:OnRemove()

	self.Entity:StopSound("Weapon_FlareGun.Burn")
end